#!/bin/bash
: << !
  version: 0.0.1
  desc: web端环境脚本
!

init_web() {
  # 项目名称
  PROJECT_NAME="web";
  # 项目目录
  PROJECT_PATH=${APP_PATH}"/"${PROJECT_NAME};
  # 项目目录
  PROJECT_LOG_PATH=${APPLOG_PATH}"/"${PROJECT_NAME};

  if [ ! -d $PROJECT_PATH ]; then
    exec_cmd "mkdir -p ${PROJECT_PATH}"
  fi;

  if [ ! -d $PROJECT_LOG_PATH ]; then
    exec_cmd "mkdir -p ${PROJECT_LOG_PATH}"
  fi;
  unset PROJECT_NAME
}

install_node() {
  EXISTING_NODE=$(rpm -qa 'node|npm|iojs' | grep nodesource)

  # 判断node是否安装
  if [ "X${EXISTING_NODE}" != "X" ]; then
    print_status "${green}[node] was installed.${normal}"
  else
    exec_cmd "curl --silent --location ${NODE_DOWNLOAD_URL} | bash - && \
    sudo yum -y install nodejs && \
    npm config set registry https://registry.npm.taobao.org/"
  fi;
}

install_resin() {
  RESIN_PATH="${APP_PATH}/${RESIN_VERSION}";

  is_resin=$(ls ${APP_PATH}/${RESIN}* 2> /dev/null | wc -l)
  # 判断resin目录是否存在
  if [ "${is_resin}" != "0" ]; then
    print_status "${green}[${RESIN}] already exists.${normal}"
  else
    print_status "resin install..."
    exec_cmd "curl -o '${RESIN_PATH}' ${RESIN_DOWNLOAD_URL}"

    if [ ! -f ${RESIN_PATH} ]; then
      print_status "${red}${RESIN_PATH} non-exists.${normal}"
      bail
    else
      exec_cmd "tar -xv -f '${RESIN_PATH}' -C ${APP_PATH} ${RESIN} && rm -f '${RESIN_PATH}'"
    fi;
  fi;
}

install_openresty() {
  EXISTING_OPENRESTY=$(rpm -qa 'openresty' | grep openresty)

  # 判断openresty是否安装
  if [ "X${EXISTING_OPENRESTY}" != "X" ]; then
    print_status "${green}[openresty] was installed.${normal}"
  else
    print_status "openresty install..."
    exec_cmd "sudo yum install yum-utils -y && \
    sudo yum-config-manager --add-repo ${OPENRESTY_DOWNLOAD_URL} && \
    sudo yum install openresty -y"
  fi;
}
